package cn.biplam.front.component.view.systemManage.modelset;

import cn.biplam.common.ajxcode.RuleEngineCode;
import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.bean.systemManage.Product.Modual_Name;
import cn.biplam.common.client.Database;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.paramters.DbsCaption;
import cn.biplam.common.paramters.EdwCaption;
import cn.biplam.common.paramters.ExtractCaption;
import cn.biplam.common.paramters.SqlRuleCaption;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.paramters.UserPriviCaption;
import cn.biplam.common.paramters.ModelType.Model_Type;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.view.systemManage.sysprivi.privilege.PriviDeployManage;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.template.control.Panel;

/**
 * 模型设置主页面
 * 设置业务对象的根主题信息
 * @author WangSong 2009-7-22下午04:03:31
 * @version 1.0
 */
public class ModelSetMain extends AjxResponse{

	public static final String cId = "8204";
	public static final String aId = "820401";

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(aId.equals(actionId)){
			return getMainPage(mUser, sHomeUrl);
		}else{
			throw new ErrorMessage("Actionid is not found:"+actionId+"!");
		}
	}

	/**
	 * 取模型设置主页面
	 * @param mUser  用户信息 
	 * @param sHomeUrl 系统路径
	 * @return HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow getMainPage(UserInfo mUser,String sHomeUrl) throws ErrorMessage{
		Panel panel = new Panel(cId+"panal");

		putSrcLabel(panel);
		putBmLabel(panel);
		putDwLabel(panel);
		putSqlRuleLabel(panel);
		putPriviLable(panel);

		return panel.get();
	}

	/**
	 *	放入源系统模型管理标签 
	 */
	private void putSrcLabel(Panel panel) throws ErrorMessage{
		if(modelIsDeploy(Modual_Name.DBS)){
			panel.put(	CaptionComp.get(mUser, DbsCaption.Src_Model_Mange), 
					ModelSetManage.aId_main, 
					Model_Type.Src.toString());
		}
	}

	/**
	 *	放入业务对象模型管理标签 
	 */
	private void putBmLabel(Panel panel) throws ErrorMessage{
		if(modelIsDeploy(Modual_Name.Extract)){
			panel.put(	CaptionComp.get(mUser, ExtractCaption.Bo_Model_Mana),
					ModelSetManage.aId_main, 
					Model_Type.Bm.toString());
		}
	}

	/**
	 *	放入数据仓库模型管理标签 
	 */
	private void putDwLabel(Panel panel) throws ErrorMessage{
		if(modelIsDeploy(Modual_Name.EDW)){
			panel.put(	CaptionComp.get(mUser,EdwCaption.Edw_Model_Mana),
					ModelSetManage.aId_main, 
					Model_Type.Dw.toString());
		}
	}

	/**
	 *	放入规则引擎模型发布标签 
	 */
	private void putSqlRuleLabel(Panel panel) throws ErrorMessage{
		if(modelIsDeploy(Modual_Name.RULEENGINE)){
			panel.put(	CaptionComp.get(mUser,SqlRuleCaption.SqlRule_Model_Mana),//caption
					RuleEngineCode.DeployManageCode.Model_Main 		//action
			);
		}
	}
	
	/**
	 *  放入权限发布管理标签
	 */
	private void putPriviLable(Panel panel) throws ErrorMessage{
		panel.put( CaptionComp.get(mUser,UserPriviCaption.PriviDeployManage_Title), 
				PriviDeployManage.Privi_Deploy_Main
				);
	}


	private boolean modelIsDeploy(Modual_Name model) throws ErrorMessage{
		StringBuilder sb = new StringBuilder("select p_service from sys_product where p_name='");
		sb.append(model);
		sb.append('\'');
		Database db=new Database(new DbResource(),SysName.SysUser,null);
		SqlResult rs = db.executeSelect(new Sql(sb.toString()));
		rs.next();
		return rs.isNull()?false:rs.getBoolean(0);
	}

}
